package com.luck.emp.product.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.luck.emp.entity.EmpTProdAss;
import com.luck.emp.entity.EmpTProdBase;
import com.luck.emp.entity.EmpTProdKind;
import com.luck.emp.entity.EmpTProdSale;
import com.luck.emp.entity.EmpTProdVend;
import com.luck.emp.entity.EmpTVendor;
import com.luck.emp.product.logic.IProductLogic;
import com.luck.emp.product.qry.ProductQry;
import com.luck.emp.report.logic.IReportLogic;
import com.luck.emp.report.qry.ReportQry;
import com.luck.emp.report.vo.StorageVo;
import com.luck.emp.vendor.logic.IVendorLogic;
import com.luck.emp.vendor.qry.VendorQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class ProductAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IProductLogic productLogic;
		IVendorLogic vendorLogic;
		IReportLogic reportLogic;
		/**
		 * 查询对象
		 */
		ProductQry qry;
		ReportQry reportQry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTProdBase entity;
		EmpTProdSale sale;
		Long kindId;//分类
		
		List<EmpTVendor> vendors;
		List<EmpTProdVend> prodVends;//供应商关系
		String[] vendorCodes;
		List<EmpTProdAss> prodAss;//组装关系
		String[] productCodeSubs;//子商品数组
		String[] qtys;//数量数组
		List<StorageVo> vos;
		
		public String index() throws Exception{
			return "index";
		}
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ProductQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ProductQry();
			}
			if(qry.getKindId()!=null)
				kindId = qry.getKindId();
			qry.setUserInfo(userInfo);
			page = productLogic.findProductPage(qry,Page.class);
			return "list";
		}
		/**
		 * 提供弹出窗口选择商品
		 * @return
		 * @throws Exception
		 */
		public String selectCg() throws Exception{
			UserBean userInfo = super.getUserInfo();
			
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ProductQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ProductQry();
			}
			if(qry.getKindId()!=null)
				kindId = qry.getKindId();
			qry.setUserInfo(userInfo);
			qry.setOrgId(userInfo.getOrgId());
			page = productLogic.findProductPage(qry,Page.class);
			return "selectCg";
		}
		public String selectAss() throws Exception{
			UserBean userInfo = super.getUserInfo();
			
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ProductQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ProductQry();
			}
			if(qry.getKindId()!=null)
				kindId = qry.getKindId();
			qry.setUserInfo(userInfo);
			qry.setOrgId(userInfo.getOrgId());
			page = productLogic.findProductPage(qry,Page.class);
			return "selectAss";
		}
		/**
		 * 出入库选择商品
		 * @return
		 * @throws Exception
		 */
		public String selectWh() throws Exception{
			UserBean userInfo = super.getUserInfo();
			
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ProductQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ProductQry();
			}
			if(qry.getKindId()!=null)
				kindId = qry.getKindId();
			qry.setUserInfo(userInfo);
			qry.setOrgId(userInfo.getOrgId());
			page = productLogic.findProductPage(qry,Page.class);
			return "selectWh";
		}
		public String select() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ProductQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ProductQry();
			}
			if(qry.getKindId()!=null)
				kindId = qry.getKindId();
			qry.setUserInfo(userInfo);
			qry.setOrgId(userInfo.getOrgId());
			page = productLogic.findProductPage(qry,Page.class);
			return "select";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(entity == null)
				entity = new EmpTProdBase();
			if(entity.getProdKindId()!=null)
				entity.setProdKind(productLogic.get(EmpTProdKind.class, entity.getProdKindId()));
			super.event="ADD";
			super.title="新增商品信息";
			return "editBase";
		}

		/**
		 * 修改主界面
		 **/
		public String editAll() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = productLogic.get(EmpTProdBase.class,entity.getProductCode());
			return "editAll";
		}
		/**
		 * 修改
		 **/
		public String toEditBase() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = productLogic.get(EmpTProdBase.class,entity.getProductCode());
			super.event="EDIT";
			super.title="修改商品信息";
			return "editBase";
		}
		/**
		 * 修改
		 **/
		public String toEditSale() throws Exception{
			UserBean userInfo = super.getUserInfo();
			sale = productLogic.get(EmpTProdSale.class,entity.getProductCode());
			super.event="EDIT";
			super.title="修改商品销售信息";
			return "editSale";
		}
		/**
		 * 修改动作
		 **/
		public String editBase() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str = "";
			if(StringUtils.isBlank(entity.getProdType()))
				entity.setProdType("1");
			if("ADD".equals(super.event)){
				//新增操作
				entity.setProductCode(null);
				try {
					str = productLogic.saveProduct(userInfo, entity);
				} catch (Exception e) {
					str = e.getMessage();
				}
				super.addAttribute("qry.pk", entity.getProductCode());
			}else if("EDIT".equals(super.event)){
				//修改操作
				EmpTProdBase db = productLogic.get(EmpTProdBase.class,entity.getProductCode());
				super.copyProperties(db, entity);
				try {
					str = productLogic.saveProduct(userInfo, db);
				} catch (Exception e) {
					str = e.getMessage();
				}
				super.addAttribute("qryHex", super.qryHex);
			}
			//super.redirectUrl = "/emp/product_list.action";
			super.title="修改商品销售信息";
			
			if(StringUtils.isNotBlank(str)){
				msgStr = str;
				return GLOBAL_ERROR;
			}
			if("ADD".equals(super.event)){
				super.redirectUrl="/emp/product_editAll.action?entity.productCode="+entity.getProductCode();
				return GLOBAL_SUCCESS;
			}
			return "editBase";
		}
		/**
		 * 修改动作
		 **/
		public String editSale() throws Exception{
			UserBean userInfo = super.getUserInfo();
			//修改操作
			EmpTProdSale db = productLogic.get(EmpTProdSale.class,entity.getProductCode());
			super.copyProperties(db, sale);
			productLogic.saveProductSale(userInfo, db);
			super.addAttribute("qryHex", super.qryHex);
			sale.setProductCode(entity.getProductCode());
			super.redirectUrl="/emp/product_toEditSale.action?entity.productCode="+entity.getProductCode();
			return GLOBAL_SUCCESS;
			//return "editSale";
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			

			UserBean userInfo = super.getUserInfo();
			entity = productLogic.get(EmpTProdBase.class,entity.getProductCode());
			String str = "";
			try {
				str = this.productLogic.deleteProduct(userInfo, entity);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				msgStr = str;
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;
		}
		/**
		 * 商品供应商关系设置
		 * @return
		 * @throws Exception
		 */
		public String toProdVend() throws Exception{
			UserBean userInfo = super.getUserInfo();
			VendorQry vendorQry = new VendorQry();
			vendorQry.setUserInfo(userInfo);
			vendors = vendorLogic.findVendorPage(vendorQry, List.class);
			entity = productLogic.get(EmpTProdBase.class, entity.getProductCode());
			qry = new ProductQry();
			qry.setProductCode(entity.getProductCode());
			prodVends = productLogic.findProdVendRel(qry);
			super.title="修改商品供应商关系";
			return "vendProd";
		}
		public String prodVend() throws Exception{
			String str = "";
			try {
				productLogic.saveProdVendRel(entity.getProductCode(), vendorCodes);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				msgStr = str;
				return GLOBAL_ERROR;
			}		
			return toProdVend();
		}

		/**
		 * 商品组装关系设置
		 * @return
		 * @throws Exception
		 */
		public String toProdAss() throws Exception{
			prodAss = productLogic.findProdAssRel(entity.getProductCode());
			return "prodAss";
		}
		public String prodAss() throws Exception{
			String str = "";
			try {
				str = productLogic.saveProdAssRel(entity.getProductCode(), productCodeSubs, qtys);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				msgStr = str;
				return GLOBAL_ERROR;
			}
			super.redirectUrl="/emp/product_toProdAss.action?entity.productCode="+entity.getProductCode();
			return GLOBAL_SUCCESS;
		}
		public ProductQry getQry(){
			return this.qry;
		}
		public void setQry(ProductQry qry){
			this.qry = qry;
		}
		
		public IProductLogic getProductLogic(){
			return this.productLogic;
		}
		public void setProductLogic(IProductLogic productLogic){
			this.productLogic = productLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTProdBase getEntity(){
			return entity;
		}
		public void setEntity(EmpTProdBase entity){
			this.entity = entity;
		}
		public EmpTProdSale getSale() {
			return sale;
		}
		public void setSale(EmpTProdSale sale) {
			this.sale = sale;
		}
		public Long getKindId() {
			return kindId;
		}
		public void setKindId(Long kindId) {
			this.kindId = kindId;
		}
		public IVendorLogic getVendorLogic() {
			return vendorLogic;
		}
		public void setVendorLogic(IVendorLogic vendorLogic) {
			this.vendorLogic = vendorLogic;
		}
		public List<EmpTVendor> getVendors() {
			return vendors;
		}
		public void setVendors(List<EmpTVendor> vendors) {
			this.vendors = vendors;
		}

		public List<EmpTProdVend> getProdVends() {
			return prodVends;
		}
		public void setProdVends(List<EmpTProdVend> prodVends) {
			this.prodVends = prodVends;
		}
		public String[] getVendorCodes() {
			return vendorCodes;
		}
		public void setVendorCodes(String[] vendorCodes) {
			this.vendorCodes = vendorCodes;
		}
		public List<EmpTProdAss> getProdAss() {
			return prodAss;
		}
		public void setProdAss(List<EmpTProdAss> prodAss) {
			this.prodAss = prodAss;
		}
		public String[] getProductCodeSubs() {
			return productCodeSubs;
		}
		public void setProductCodeSubs(String[] productCodeSubs) {
			this.productCodeSubs = productCodeSubs;
		}
		public String[] getQtys() {
			return qtys;
		}
		public void setQtys(String[] qtys) {
			this.qtys = qtys;
		}
		
}